package com.leyou.order.config;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfigImpl;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 虎哥
 */
@Configuration
public class WxConfig {

    /**
     * 读取配置文件，加载属性到WxPayConfigImpl
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "ly.pay.wx")
    public WXPayConfigImpl payConfig(){
        return new WXPayConfigImpl();
    }

    /**
     * 创建WxPay对象，并且使用WXPayConfigImpl完成配置
     * @return
     */
    @Bean
    public WXPay wxPay(){
        try {
            return new WXPay(payConfig());
        } catch (Exception e) {
            throw new RuntimeException("构建WxPay工具失败！", e);
        }
    }
}
